package com.nanshenycd.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nanshenycd.pojo.Type;
import com.nanshenycd.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@Api(description  = "后台分类管理接口")
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    @ApiOperation(value = "展示渲染所有的标签分类")
    //通过PageableDefault指定每页数据，排序方式,正序倒序
    public String list(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Type> types = typeService.listType();

        //得到分页结果对象
        PageInfo<Type> pageInfo = new PageInfo<>(types);

        //将查询出来的每页的数据封装到model中
        model.addAttribute("pageInfo", pageInfo);
        return "admin/types";
    }

    @GetMapping("/types/input")
    @ApiOperation(value = "跳转分类新增页面")
    public String toAdd(Model model) {
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    @PostMapping("/types/add")
    @ApiOperation(value = "分类信息校验接口")
    public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes) {
        //重复校验
        Type type2 = typeService.getTypeByName(type.getName());
        if (type2 != null) {
            attributes.addFlashAttribute("message", "该分类已经存在");
            return "redirect:/admin/types/input";
        }
        //@Valid 代表需要校验是否为空 ,BindingResult 代表检验后的结果
        if (result.hasErrors()) {
            return "admin/types-input";
        }
        int i = typeService.saveType(type);
        if (i < 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "添加失败");
        } else {
            attributes.addFlashAttribute("message", "添加成功");
        }
        return "redirect:/admin/types";
    }


    //修改
    @GetMapping("/types/{id}/input")
    @ApiOperation(value = "返回查询后的片段信息")
    public String editInput(@PathVariable Long id, Model model) {
        Type type = typeService.getTypeById(id);
        model.addAttribute("type", type);
        return "admin/types-update";
    }

    //修改操作
    @PostMapping("/types/update")
    @ApiOperation(value = "返回查询后的片段信息")
    public String editPost(@Valid Type type, BindingResult result, RedirectAttributes attributes, Model model) {
        //重复校验
        Type type2 = typeService.getTypeByName(type.getName());
        if (type2 != null) {
//            attributes.addFlashAttribute("message", "该分类已经存在");
            model.addAttribute("message","请勿输入已有分类");
            return "/admin/types-update";
        }

        int i = typeService.updateType(type);
        if (i < 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/types";
    }


    @GetMapping("/types/{id}/delete")
    @ApiOperation(value = "返回查询后的片段信息")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        typeService.deleteType(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/types";
    }
}
